<script>
    $(function(){
        $("#context_menu").menu({
            onClick : function (item) {
                var data = $(this).data('rowData');
                switch (item.name)
                {
                    case 'right_refresh': {
                        $('#user_list_grid').datagrid('reload');
                        break;
                    }
                    case 'right_edit': {
                        checkUnique(data.ID);
                        data.Password = '';
                        $('#user_form').form('clear');
                        show_edit_win('编辑用户('+ data.Name+')',data);
                        $('#user_form').form('load',data);
                        break;
                    }
                    case 'right_delete':{
                        if (data.ID == 1)
                        {
                            $.messager.alert( '警告','该角色不能被删除', 'warning');
                            return;
                        }
                        Tool.delRequest({id:data.ID}, 'backend.php?r=user/delete',function(result){
                            if (result.success)
                            {
                                $('#user_list_grid').datagrid('reload');
                                $.messager.alert( '系统提示',result.msg, 'info');
                            }
                        });
                    }
                
                }
            }
        });
        $('#user_list_grid').datagrid({
            title: '用户列表',
            fit:true,
            singleSelect:true, 
            rownumbers:true,
            nowrap: false,
            striped: true,
            remoteSort: false,
            fitColumns: true,
            pagination:true,
            rownumbers:true,
            url:'backend.php?r=user/list',
            columns: [[                 
                    { field: 'Name', title: '名称',width:250,align: 'center' },
                    { field: 'Email', title: '邮箱',width:250,  align: 'center' }, 
                    { field: 'Status', title: '状态',width:250,  align: 'center',formatter:function(v){
                        return (v==1)?'正常':'未激活';
                    } }, 
                    { field: 'LastLogin', title: '最后登录时间',width:250,  align: 'center' }
                ]],
           toolbar:[{
                    id:'btnreload',
                    text:'刷新',
                    iconCls:'icon-reload',
                    handler:function(){
                        $('#user_list_grid').datagrid('reload');
                    }
                }, '-',{
                    id:'btnadd',
                    text:'添加用户',
                    iconCls:'icon-add',
                    handler:function(){
                        checkUnique('');
                        $('#user_form').form('clear');
                    
                        show_edit_win('添加用户',{});
                        $('#user_form').form('load',{Status:1});

                    }
                }, '-',{
                    id:'btnadd',
                    text:'编辑用户',
                    iconCls:'icon-edit',
                    handler:function(){
                        var data = $('#user_list_grid').datagrid('getSelected');
                        if (!data) {
                            $.messager.alert( '系统提示','请先选择数据', 'warning');
                            return;
                        }
                        checkUnique(data.ID);
                        $('#user_form').form('clear');
                        data.Password = '';
                        show_edit_win('编辑用户('+ data.Name+')',data);
                        $('#user_form').form('load',data);
                    }
                }, '-', {
                    id:'btnremove',
                    text:'删除',
                    iconCls:'icon-remove',
                    handler:function(){
                        var selected = $('#user_list_grid').datagrid('getSelected');
                        if (selected){
                            if (selected.ID == 1)
                            {
                                $.messager.alert( '警告','该角色不能被删除', 'warning');
                                return;
                            }
                            Tool.delRequest({id:selected.ID}, 'backend.php?r=user/delete',function(result){
                                if (result.success)
                                {
                                    $('#user_list_grid').datagrid('reload');
                                    $.messager.alert( '系统提示',result.msg, 'info');
                                }
                            });
                        }
                        else
                        {
                            $.messager.alert( '系统提示','请先选择数据', 'warning');	 
                        } 
                    }
                }],
            onRowContextMenu:function(e, rowIndex, rowData) {
            e.preventDefault();        
                $('#context_menu').menu('show',{
                    left: e.pageX,
                    top: e.pageY
                }).data('rowData',rowData);
            }
        });
    })
    function checkUnique(id) {
        var validate_content = "remote['backend.php?r=user/checkUnique/id/"+id+"','Name']";
        $('#user_name').validatebox({
        	required:"true" ,
            validType: validate_content,
            invalidMessage:"该用户名已存在" 
        });
    }

    function show_edit_win(title,data) {
        $('#edit_win').dialog({
            title : title,
            height : 300,
            width : 500,
            modal : true,
            closeAction:'destroy',
            buttons : [{
                text:'保存',
                iconCls:'icon-save',
                handler:function(){
                    if (!$('#user_form').form('validate')) return;
                    var url = 'backend.php?r=user/create';
                    submitForm('user_form',url,'edit_win','user_list_grid');
                }
            },{
                text:'取消',
                iconCls:'icon-cancel',
                handler:function(){
                    $('#edit_win').dialog('close');
                }
            }],
            onLoad:function(){
               
            }
        });
    }
</script>

<div id="manage_user" class="easyui-panel" data-options="fit:true">
        <table id='user_list_grid'></table>
</div>

<div id='edit_win'>
    <form id="user_form" method="post" novalidate>
         <div style='margin:10px 10px 0px 10px;'>
            <input class="easyui-validatebox" type="text" name="ID" id='user_id' hidden='true'></input>
        </div>
        <div style='margin:10px 10px 0px 58px;'>
            <label for="Name">用户名称:</label>
            <input class="easyui-validatebox" type="text" name="Name" id='user_name' ></input>
        </div>
        <div style='margin:10px 10px 0px 75px;'>
            <label for="Email">Email:</label>
            <input class="easyui-validatebox"  name="Email" id='user_email' data-options="required:true,validType:'email'"></input>
        </div>
        <div style='margin:10px 10px 0px 80px;'>
            <label for="Password">密码:</label>
            <input class="easyui-validatebox" type='password' name="Password" id='user_pass' data-options="required:true,validType:'minLength[6]'"></input>
        </div>
        <div style='margin:10px 10px 0px 58px;' id='pass_agin_div'>
            <label for="PasswordAgin">确认密码:</label>
            <input class="easyui-validatebox" type='password' name="Password_agin" id='user_pass_agin' required="required" validType="equals['#user_pass']"></input>
        </div>
        <div style='margin:10px 10px 0px 10px;'>
            <input class="easyui-validatebox" type="text" name="Status" hidden='true'></input>
        </div>
    </form>
</div>
<div id="context_menu" class="easyui-menu" style="width:120px">
    <div name='right_refresh'>刷新</div>
    <div class="menu-sep"></div> 
    <div name='right_edit'>编辑</div>
    <div class="menu-sep"></div> 
    <div name='right_delete'>删除</div>
</div>